package edu.bupt.c1array;

public class T19Candy {
    public int candy(int[] ratings) {
        if (ratings.length<=1){
            return ratings.length;
        }

        int[] togive = new int[ratings.length];

        int increace = 1;
        for (int i=1;i<ratings.length;i++){
            if (ratings[i] > ratings[i-1]){
                togive[i] = increace++;
            }else{
                increace = 1;
            }
        }

        increace = 1;
        for (int i=ratings.length -2;i>=0;i--){
            if (ratings[i]>ratings[i+1]){
                togive[i] = Math.max(increace++, togive[i]);
            }else{
                increace = 1;
            }
        }

        int sum=ratings.length;
        for (int i:togive){
            sum += i;
        }
        return sum;


    }
}
